--编写者：alt
--功能介绍：免疫减速
--使用方法：直接粘贴到模组环境 修改muu_nospeed为对应tag 涉及到主客问题 需要用tag

--减速
--背负重物和装备的减速效果不会使顺奈减速
local equippable = require("components/equippable")
local oldGetWalkSpeedMult = equippable.GetWalkSpeedMult
function equippable:GetWalkSpeedMult(...)
    local oldmult = oldGetWalkSpeedMult(self, ...)
    local inventoryitem = self.inst.components.inventoryitem
    local owner = inventoryitem and inventoryitem:GetGrandOwner()
    if owner and owner:HasTag("muu_nospeed") and oldmult and oldmult < 1 then
        oldmult = 1
    end
    return oldmult
end

--延迟补偿的处理
local InventoryItem = require("components/inventoryitem_replica")
local oldinvGetWalkSpeedMult = InventoryItem.GetWalkSpeedMult
function InventoryItem:GetWalkSpeedMult(...)
    local oldmult = oldinvGetWalkSpeedMult(self, ...)
    local owner = self.inst and self.inst.entity:GetParent()
    if owner and owner:HasTag("muu_nospeed") and oldmult and oldmult < 1 then
        oldmult = 1
    end
    return oldmult
end

local LocoMotor = require("components/locomotor")
local oldTempGroundSpeedMultiplier = LocoMotor.TempGroundSpeedMultiplier
function LocoMotor:TempGroundSpeedMultiplier()
    local old = oldTempGroundSpeedMultiplier(self)
    local owner = self.inst
    if owner and owner:HasTag("muu_nospeed") then
        return 1
    end
    return old
end

local oldSetExternalSpeedMultiplier = LocoMotor.SetExternalSpeedMultiplier
function LocoMotor:SetExternalSpeedMultiplier(source, key, m)
    local owner = self.inst
    if owner and owner:HasTag("muu_nospeed")
        and m and m < 1 then
        return oldSetExternalSpeedMultiplier(self, source, key, 1)
    end
    return oldSetExternalSpeedMultiplier(self, source, key, m)
end

local StormWatcher = require("components/stormwatcher")
local oldCheckStorms = StormWatcher.CheckStorms
function StormWatcher:CheckStorms(...)
    local owner = self.inst
    if owner and owner:HasTag("muu_nospeed") then
        self.stormlevel = 0
        self.currentstorm = STORM_TYPES.NONE
        return
    end
    return oldCheckStorms(self, ...)
end
